#可迭代对象，实现了__iter__方法的对象 字符串，等都是可迭代对象
#迭代器，实现了__iter__，__next__方法的对象 列表等是迭代器

class Myiter():
    def __init__(self,array):
        self.value=array
        self.index=0
    def __iter__(self):
        return self
    def __next__(self):
        if self.index< len(self.value):
            value=self.value[self.index]
            self.index=self.index+1
        else:
            raise StopIteration
        return value
example=Myiter([1,2,3,4])
# print(type(example))
# print(next(example))


#生成器
def gene():
    print("code start")
    for i in range(5):
        print("{} start".format(i))
        yield i
        print("{} end".format(i))
    print("code end")

gen=gene()
print(next(gen))
print('*****1*******')
print(next(gen))
print('*****2*******')
print(next(gen))
print('*****3*******')
print(next(gen))
print('*****4*******')
print(next(gen))
print('*****5*******')
print(next(gen))
print('*************')


# code start
# 0 start
# 0
# *****1*******
# 0 end
# 1 start
# 1
# *****2*******
# 1 end
# 2 start
# 2
# *****3*******
# 2 end
# 3 start
# 3
# *****4*******
# 3 end
# 4 start
# 4
# *****5*******
# 4 end
# code end
# Traceback (most recent call last):
#   File "f:\lookjob\myiter.py", line 42, in <module>
#     print(next(gen))
# StopIteration



# print(next(gen))
# print(next(gen))
# print(next(gen))
# print(next(gen))

#列表生成器
a=(i for i in range(100))
